package com.regex.web.controller.community;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.regex.web.entity.community.CommunityTypeDTO;
import com.regex.web.entity.community.NoticeDTO;
import com.regex.web.entity.community.NoticeLikeDTO;
import com.regex.web.entity.member.MemberDTO;
import com.regex.web.service.community.ICommunityTypeService;
import com.regex.web.service.community.INoticeLikeService;
import com.regex.web.service.community.INoticeService;
import com.regex.web.service.member.IMemberService;
import com.regex.web.utils.Contant;
import com.regex.web.utils.QueryParam;
import com.regex.web.utils.QueryResult;

@Controller
@EnableAutoConfiguration
@RequestMapping("sys/noticeLike")
public class SysNoticeLikeController {


    @Autowired
    private INoticeLikeService noticeLikeService;
    
    @Autowired
    private INoticeService noticeService;
    
//    @Autowired
//    private IVillageService villageService;
    
    @Autowired
    private ICommunityTypeService communityTypeService;
    
    @Autowired
    private IMemberService memberService;
    
    @RequiresPermissions("noticeLike:show")
    @RequestMapping("show")
    public String show(QueryParam param, Model model, String noticeId) {
        NoticeDTO noticeDTO = noticeService.getById(Long.parseLong(noticeId));
        CommunityTypeDTO communityTypeDTO = communityTypeService.getById(Long.parseLong(noticeDTO.getType()));
        if(null != communityTypeDTO) {
            noticeDTO.setTypeName(communityTypeDTO.getName());
        }
//        VillageDTO villageDTO = villageService.getById(noticeDTO.getVillageId());
//        if(villageDTO != null) {
//            noticeDTO.setVillageName(villageDTO.getName());
//        }
        if(param.getPageSize() <= 0) {
            param.setPageSize(Contant.NONMAL_PAGE_SIZE);
        }
        if(param.getPageNumber() < 1) {
            param.setPageNumber(1);
        }
        
        Page<NoticeLikeDTO> page = new Page<NoticeLikeDTO>();
        page.setCurrent(param.getPageNumber());
        page.setSize(param.getPageSize());
        QueryWrapper<NoticeLikeDTO> param1 = new QueryWrapper<NoticeLikeDTO>();
        param1.eq("notice_id", noticeId);
        param1.orderByDesc("create_time");
        QueryResult<NoticeLikeDTO> result = noticeLikeService.page(page, param1);
        for(NoticeLikeDTO noticeLikeDTO : result.getDatas()) {
            MemberDTO memberDTO = memberService.getById(noticeLikeDTO.getMemberId());
            noticeLikeDTO.setNickName(memberDTO.getNickName());
        }
        model.addAttribute("result", result);
        model.addAttribute("noticeDTO", noticeDTO);
        model.addAttribute("noticeId", noticeId);
        return "sys/noticeLike/show";
    }

}
